import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'

/**
 * Note: sub-menu only appear when route children.length >= 1
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                   if set true, item will not show in the sidebar(default is false)
 * alwaysShow: true               if set true, will always show the root menu
 *                                if not set alwaysShow, when item has more than one children route,
 *                                it will becomes nested mode, otherwise not show the root menu
 * redirect: noRedirect           if set noRedirect will no redirect in the breadcrumb
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * meta : {
    roles: ['admin','editor']    control the page roles (you can set multiple roles)
    title: 'title'               the name show in sidebar and breadcrumb (recommend set)
    icon: 'svg-name'/'el-icon-x' the icon show in the sidebar
    breadcrumb: false            if set false, the item will hidden in breadcrumb(default is true)
    activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
  }
 */

/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */
export const constantRoutes = [
  {
    path: '/login',
    component: () => import('@/views/login/login'),
    hidden: true
  },

  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },

  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [{
      path: 'dashboard',
      name: 'Dashboard',
      component: () => import('@/views/dashboard/DashBoard'),
      meta: { title: '作业看板', icon: 'dashboard' }
    }]
  },

  {
    path: '/task',
    component: Layout,
    redirect: '/task/setMark',
    name: 'Plan',
    meta: { title: '任务计划', icon: 'el-icon-s-help' },
    children: [
      {
        path: 'setMark',
        name: 'SetMark',
        component: () => import('@/views/task/SetMark'),
        meta: { title: '设置航标' }
      },
      {
        path: 'adjustMark',
        name: 'AdjustMark',
        component: () => import('@/views/task/ChangeMark'),
        meta: { title: '调整航标' }
      },
      {
        path: 'removeMark',
        name: 'RemoveMark',
        component: () => import('@/views/task/RemoveMark'),
        meta: { title: '撤除航标' }
      },
      {
        path: 'markQuality',
        name: 'MarkQuality',
        component: () => import('@/views/task/CheckMark'),
        meta: { title: '航标巡检' }
      },
      {
        path: 'emergency',
        name: 'Emergency',
        component: () => import('@/views/task/MaintainTask'),
        meta: { title: '应急维修' }
      },
      {
        path: 'retestMark',
        name: 'RetestMark',
        component: () => import('@/views/task/RetestMark'),
        meta: { title: '位置复测' }
      },
      {
        path: 'techMeasure',
        name: 'TechMeasure',
        component: () => import('@/views/task/TechMesaure'),
        meta: { title: '位置测定' }
      }
    ]
  },

  {
    path: '/sysSettings',
    component: Layout,
    redirect: '/sysSettings/userManager',
    name: 'SysSettings',
    meta: {
      title: '系统设置',
      icon: 'nested'
    },
    children: [
      {
        path: 'navigationTypeManager',
        component: () => import('@/views/settings/NavigationTypeManager'),
        name: 'NavigationTypeManager',
        meta: { title: '航标类型管理' }
      },
      {
        path: 'attributeManager',
        component: () => import('@/views/settings/AttributeManager'),
        name: 'AttributeManager',
        meta: { title: '航标属性管理' }
      },
      {
        path: 'divisionNameManager',
        component: () => import('@/views/settings/DivisionNameManager'),
        name: 'DivisionNameManager',
        meta: { title: '所属公司管理' }
      },
      {
        path: 'channelManager',
        component: () => import('@/views/settings/ChannelManager'),
        name: 'ChannelManager',
        meta: { title: '港口管理' }
      },
      {
        path: 'seaRouteManager',
        component: () => import('@/views/settings/SeaRouteManager'),
        name: 'SeaRouteManager',
        meta: { title: '航道管理' }
      },
      {
        path: 'areaManager',
        component: () => import('@/views/settings/AreaManager'),
        name: 'AreaManager',
        meta: { title: '海区管理' }
      },
      {
        path: 'userManager',
        component: () => import('@/views/settings/UserManager'),
        name: 'UserManager',
        meta: { title: '终端用户管理' }
      }
    ]
  },

  // 404 page must be placed at the end !!!
  { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
